﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Student.Models;
using PagedList;

namespace OT.Areas.Student.Controllers
{
    public class AttemptController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex, string columnFilter)
        {
            var Attempts = (List<AttemptMsts>)null;
            int recount = 0;
            try
            {
                int UserID = 0;
                if (Request.Cookies["LoginSesID"] != null)
                {
                    UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
                }
                else
                {
                    TempData["notice"] = "Time Out ,Please Login";
                    Response.Redirect("~/Home");
                }

                Attempts = (from c in OTDb.AttemptMsts
                            join top in OTDb.Tests
                            on c.TestID equals top.TestID
                            where c.UserID == UserID
                            select new AttemptMsts
                            {
                                AttemptID = c.AttemptID,
                                IsFinish = (bool)c.IsFinish,
                                Score = (double?)c.Score ?? 0, //set optional value for 0 if score is null
                                CreatedDate = (c.CreatedDate.Value == null) ? DateTime.Now :c.CreatedDate.Value,
                                TestID = (int)c.TestID,
                                TestName = top.TestName,
                            }).OrderByDescending(o=>o.CreatedDate).ToList() ; 

                if (!String.IsNullOrEmpty(searchString))
                    Attempts = Attempts.Where(r => r.TestName.Contains(searchString)).ToList();

                recount = Attempts.Count;

                Attempts = Attempts.Skip<AttemptMsts>(pgindex * (pageNumber - 1))
                    .Take<AttemptMsts>(pgindex).ToList<AttemptMsts>();

            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }

            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = Attempts,
                RecordCount = recount
            });

        }
        #endregion

        #region Delete
        public ActionResult Delete(int AttemptID)
        {

            AttemptMsts AttemptMst = (from att in OTDb.AttemptMsts
                                      where att.AttemptID.Equals(AttemptID)
                                      select new AttemptMsts { AttemptID = att.AttemptID }).FirstOrDefault();

            return PartialView(AttemptMst);
        }

        [HttpPost]
        public string Delete(int AttemptID, string IsDelete)
        {
            string result = "";
            try
            {
                AttemptMst Attemptmst = OTDb.AttemptMsts.Where(r => r.AttemptID == AttemptID).FirstOrDefault();

                if (Attemptmst != null)
                {
                    OTDb.AttemptMsts.Remove(Attemptmst);
                    OTDb.SaveChanges();

                }
                result = "Success";
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
                result = "NotValid";
            }
            return result;
        }
#endregion

        #region DeleteAll
        [HttpPost]
        public string DeleteAll(string selectedRecord)
        {
            string result = "";
            string[] sep = { "," }; 
            string[] IDs = selectedRecord.Split(sep, StringSplitOptions.RemoveEmptyEntries);
            List<long> ids = new List<long>();

            foreach (var id in IDs)
                ids.Add(Convert.ToInt64(id));

            result = "";

            try
            {
                if (ids.Count != 0)
                {
                    foreach(long AttemptID in ids)
                    {

                        AttemptMst Attemptmst = OTDb.AttemptMsts.Where(r => r.AttemptID == AttemptID).FirstOrDefault();

                        if (Attemptmst != null)
                        {
                            OTDb.AttemptMsts.Remove(Attemptmst);
                            OTDb.SaveChanges();

                        }
                    }
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
                result = "NotValid";
            }
            return result;
        }
        #endregion

    }
}
